﻿using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace component
{
    [Transaction(TransactionMode.Manual)]
    internal class RfaClass : IExternalCommand
    {
        Family family = null;
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Document doc = commandData.Application.ActiveUIDocument.Document;

            string s = @"D:\混凝土梁-矩形砼梁-有标记.0001.rfa";
            Transaction tran = new Transaction(doc);
            tran.Start("载入族");
            bool b = doc.LoadFamily(s, out family);
            tran.Commit();
            if (b) { TaskDialog.Show("11", "加载成功"); }
            //Get_FamilySymbol(family);
            Element element = family as Element;
            //getSymbolpara();//获取族类型参数
            //ShowAllBuildinPara();//将族类型信息参数转成table表格
            Transaction trandelete = new Transaction(doc);//创建事物
            trandelete.Start("删除族");//开启事物                 
            trandelete.Commit();//关闭事物

            string info = "";

            // 获得族管理器
            FamilyManager familyMgr = doc.FamilyManager;
            FamilyType currType = familyMgr.CurrentType;
            if (currType != null)
            {   // 当前族类型可能为空，故需进行空检查
                FamilyParameterSet familyParamSet = familyMgr.Parameters;
                ISet<ElementId> familySymbolIds = family.GetFamilySymbolIds();



                ElementId[] ids = new ElementId[familySymbolIds.Count];
                familySymbolIds.CopyTo(ids, 0);
                var id = ids[0];
                DataTable parameter_datatable = new DataTable();
                FamilySymbol familySymbol = family.Document.GetElement(id) as FamilySymbol;
                parameter_datatable.Columns.Add("类型名称");
                foreach (FamilyParameter p in familyParamSet)
                {
                    if (p.Definition.Name == "类别" || p.Definition.Name == "类型名称")
                        continue;
                    parameter_datatable.Columns.Add(p.Definition.Name);
                }
                foreach (ElementId rowId in ids)
                {
                    FamilySymbol symbol = family.Document.GetElement(rowId) as FamilySymbol;
                    DataRow row = parameter_datatable.NewRow();
                    row["类型名称"] = symbol.Name;
                    foreach (FamilyParameter p in familyParamSet)
                    {
                        if (p.Definition.ParameterType == ParameterType.Length)
                        {
                            double dbl1 = currType.AsDouble(p).Value;
                            double d = UnitUtils.Convert(dbl1, DisplayUnitType.DUT_DECIMAL_FEET, DisplayUnitType.DUT_METERS);
                            row[p.Definition.Name] = d;
                        }
                        else
                        {
                            row[p.Definition.Name] = "-";
                        }
                    }
                    parameter_datatable.Rows.Add(row);
                }
                string json_format_string = Newtonsoft.Json.JsonConvert.SerializeObject(parameter_datatable);
                BsonDocument document = BsonDocument.Parse(json_format_string);
                //json_format_string as BsonElement
                //弹窗展示json格式参数信息
                TaskDialog.Show("Result", json_format_string);
                string connStr = "mongodb://127.0.0.1:27017";

                //2.创建连接MongoClient实例实际上代表了一个到数据库的连接池，即使有多个线程，也只需要一个MongoClient类的实例
                var client = new MongoClient(connStr);

                //3.使用client的GetDatabase方法获取数据库，即使该数据库不存在，也会自动创建；
                IMongoDatabase db = client.GetDatabase("test");
                //4.获取数据集 collection；BsonDocument是在数据没有预先定义好的情况下使用的。
                IMongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("runoob1");
                //5.插入一条数据;

                
                collection.InsertOne(document);

                //测试用下载txt格式文件
                StreamWriter sw = new StreamWriter("D:\\1.txt", false);
                sw.WriteLine(json_format_string);
                sw.Close();
                TaskDialog.Show("Result", "保存成功");
                MessageBox.Show(info, "信息");

            }
            return Result.Succeeded;
        }
    }
}
